import * as apis from 'services/order'

const types = {
  GET_ORDERCONFITMATION_SUCCESS: 'GET_ORDERCONFITMATION_SUCCESS',
}

const getters = {
  addressIsEmpty(state) {
    return typeof state.orderConfirmationData.address.addressId === 'undefined'
  },
  orderPirce(state) {
    const currencySymbol = state.orderConfirmationData.currencySymbol
    const payAmount = state.orderConfirmationData.order.exchangeOrderAmount
    return `
      ${currencySymbol} ${payAmount}
    `
  },
  orderPriceInfo(state) {
    const array = []
    if (state.orderConfirmationData.items) {
      array.push(Object.assign({
        leftTitle: `Product ( ${state.orderConfirmationData.items.length} items )`,
        isRed: 0,
        rightTitle: `${state.orderConfirmationData.currencySymbol} ${state.orderConfirmationData.order.exchangeOrderAmount}`,
      }))
    }
    array.push(Object.assign({ leftTitle: 'Shipping', isRed: 1, rightTitle: 'Free Shipping' }))
    array.push(Object.assign({
      leftTitle: 'Coupons Discount',
      isRed: 1,
      rightTitle: `-${state.orderConfirmationData.currencySymbol} ${state.orderConfirmationData.order.exchangeCouponAmount}`,
    }))

    array.push(Object.assign({
      leftTitle: 'Total',
      isRed: 0,
      rightTitle: `${state.orderConfirmationData.currencySymbol} ${state.orderConfirmationData.order.exchangeOrderAmount}`,
    }))
    return array
  },
}

const state = {
  orderConfirmationData: {
    order: {
      address: {},
    },
  },
}

const actions = {
  getOrderConfirmation({ commit, dispatch }, payload) {
    return apis.getOrderConfirmation(payload)
      .then((res) => {
        if (res.code === 1000) {
          commit(types.GET_ORDERCONFITMATION_SUCCESS, res.data)
        }
        return res
      })
  },
  createOrder({ commit }, payload) {
    return apis.createOrder(payload)
  },
  validationPay({ commit }, payload) {
    console.log(payload)
    return apis.validationPay(payload)
  },
}

const mutations = {
  [types.GET_ORDERCONFITMATION_SUCCESS](state, data) {
    state.orderConfirmationData = data
  },
}

export default {
  state,
  actions,
  mutations,
  getters,
}
